package floodFill;

import java.util.Arrays;

public class Solution {
    private int [][]arr;
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        arr = new int[image.length][image[0].length];
        dfs(image,sr,sc,color,image[sr][sc]);
        for (int i = 0; i < image.length; i++) {
            System.out.println(Arrays.toString(image[i]));
        }
        return image;
    }
    private void dfs(int[][] image,int sr,int sc,int color,int target){
        // 上下左右周围四个点都要判断是否相同
        // 判断是否已经遍历过
        if(arr[sr][sc] == -1){
            return;
        }
        arr[sr][sc] = -1;
        if(image[sr][sc] != target){
            return;
        }
        image[sr][sc] = color;

        if(sr-1>=0){
            dfs(image,sr-1,sc,color,target);
        }
        if(sr+1<image.length){
            dfs(image,sr+1,sc,color,target);
        }
        if(sc-1>=0){
            dfs(image,sr,sc-1,color,target);
        }
        if(sc+1<image[0].length){
            dfs(image,sr,sc+1,color,target);
        }
    }
}
